﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DTO;
using BLL;

namespace HDT.GUI
{
    public partial class FrmSearchMH : Form
    {
        private string PRODUCT_IMAGE_FOLDER = "Image\\_Product\\";
        List<MatHangDTO> dsMatHang;
        List<LoaiMatHangDTO> dsLoaiMH;

        public delegate void MyDelegate(MatHangDTO matHang);
        public MyDelegate callBack;

        public FrmSearchMH()
        {
            InitializeComponent();
            dsLoaiMH = new List<LoaiMatHangDTO>();
            dsMatHang = new List<MatHangDTO>();
        }

        private void FrmSearchMH_Load(object sender, EventArgs e)
        {
            try
            {
                dsLoaiMH = new LoaiMatHangBLL().Select("TrangThai = 1");
            }
            catch { }
            LoadLoaiMH();            
            cbxLMH.SelectedIndex = 0;
            cboSXGia.SelectedIndex = 0;
            SearchMatHang();
            cbxLMH.SelectedIndexChanged += SelectedIndexChanged;
            cboSXGia.SelectedIndexChanged += SelectedIndexChanged;
        }

        void SelectedIndexChanged(object sender, EventArgs e)
        {
            SearchMatHang();
        }

        private void LoadLoaiMH()
        {
            cbxLMH.Items.Add("Tất cả");
            foreach (LoaiMatHangDTO loai in dsLoaiMH)
            {
                cbxLMH.Items.Add(loai.TenLoaiMatHang);
            }
        }
        
        private void SearchMatHang()
        {
            string condition = "TrangThai = 1 AND TenMatHang LIKE '%" + txtTenMH.Text.Trim() + "%'"
                + (cbxLMH.SelectedIndex == 0 ? "" : " AND LoaiMatHang = '" + dsLoaiMH[cbxLMH.SelectedIndex - 1].MaLoaiMatHang + "'");
            string orderBy = "TenMatHang " + (cboSXGia.SelectedIndex == 0 ? "ASC" : "DESC");
            dsMatHang = new MatHangBLL().Select(condition, orderBy);
            LoadDSMatHang();

        }

        private void LoadDSMatHang()
        {
            dgvMH.Rows.Clear();
            foreach (MatHangDTO matHang in dsMatHang)
            {
                DataGridViewRow row = (DataGridViewRow)dgvMH.Rows[0].Clone();
                row.Cells[0].Value = matHang.MaMatHang;
                row.Cells[1].Value = matHang.TenMatHang;
                try
                {
                    foreach (LoaiMatHangDTO loai in dsLoaiMH)
                    {
                        if (loai.MaLoaiMatHang == matHang.MaLoaiMatHang)
                        {
                            row.Cells[2].Value = loai.TenLoaiMatHang;
                            break;
                        }
                    }
                }
                catch { }
                row.Cells[3].Value = matHang.DonViTinh;
                dgvMH.Rows.Add(row);
            }
        }

        private void LoadProductImage(string filename)
        {
            try
            {
                string path = PRODUCT_IMAGE_FOLDER + filename;
                Image img = Image.FromFile(path);
                picMH.Image = img;
            }
            catch
            {
                picMH.Image = null;
            }
        }

        private void dgvMH_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvMH.SelectedRows.Count > 0)
            {
                try
                {
                    string MaMH = dgvMH.SelectedRows[0].Cells[0].Value.ToString();
                    LoadProductImage(new MatHangBLL().GetByID(MaMH).HinhAnh);
                }
                catch
                {
                    LoadProductImage(null);
                }
            }
        }

        private void txtTenMH_TextChanged(object sender, EventArgs e)
        {
            SearchMatHang();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (dgvMH.SelectedRows.Count > 0)
            {
                try
                {
                    string MaMH = dgvMH.SelectedRows[0].Cells[0].Value.ToString();
                    MatHangDTO m = new MatHangBLL().GetByID(MaMH);
                    callBack(m);
                }
                catch
                {

                }
            }
            this.Dispose();
        }
    }
}
